package com.sniper.web.utils;

import com.sniper.exception.SniperException;
import com.sniper.mybatis.service.BaseService;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 专门controller函数兼用
 * Created by suzhen on 2017/5/11.
 */
public class ControllerUtils {

    /**
     * 删除
     *
     * @param ajaxResult
     * @param services
     * @param delid
     */
    public static void delete(Map<String, Object> ajaxResult, BaseService services, String[] delid) {
        try {
            for (String id : delid) {
                services.delete(id);
            }
            ajaxResult.put("code", 1);
            ajaxResult.put("msg", "success");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.put("code", -1);
            ajaxResult.put("msg", "删除失败");
        }
    }

    public static void delete(Map<String, Object> ajaxResult, BaseService services, Integer[] delid) {
        try {
            for (Integer id : delid) {
                services.delete(id);
            }
            ajaxResult.put("code", 1);
            ajaxResult.put("msg", "success");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.put("code", -1);
            ajaxResult.put("msg", "删除失败");
        }
    }

    /**
     * 获取错误
     *
     * @param result
     * @param callBack
     * @return
     */
    public CallBackResult ajaxPostResult(BindingResult result, CallBack callBack) {

        CallBackResult ajaxResult = new CallBackResult();
        if (result != null && result.hasErrors()) {
            ajaxResult.setCode(500);
            ajaxResult.setMsg("Failed");
            List<String> error = new ArrayList<>();
            for (FieldError fieldError : result.getFieldErrors()) {
                error.add(fieldError.getDefaultMessage());
            }
            ajaxResult.getErrors().addAll(error);
        } else {
            try {
                callBack.load(ajaxResult);
            } catch (CallBackException e) {
                e.printStackTrace();
                ajaxResult.setCode(500);
                //捕获错误反馈
                ajaxResult.setMsg(e.getMessage());
            }
        }
        return ajaxResult;
    }


    public static class CallBackResult {
        private String id;
        private int code = 200;
        private String msg = "Success";
        private String backUrl;
        //函数回调
        private String successCallBack;
        private String failCallBack;
        List<String> errors = new ArrayList<>();

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public List<String> getErrors() {
            return errors;
        }

        public void setErrors(List<String> errors) {
            this.errors = errors;
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        public String getBackUrl() {
            return backUrl;
        }

        public void setBackUrl(String backUrl) {
            this.backUrl = backUrl;
        }


        public String getFailCallBack() {
            return failCallBack;
        }

        public void setFailCallBack(String failCallBack) {
            this.failCallBack = failCallBack;
        }

        public String getSuccessCallBack() {
            return successCallBack;
        }

        public void setSuccessCallBack(String successCallBack) {
            this.successCallBack = successCallBack;
        }
    }

    /**
     * 回调
     */
    public interface CallBack {
        CallBackResult load(CallBackResult result) throws CallBackException;
    }

    /**
     * 异常
     */
    public static class CallBackException extends SniperException {

        private static final long serialVersionUID = 5118237206673322338L;

        public CallBackException(String message) {
            super(message);
        }
    }

}
